﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace _180FrameWork.Common
{
    public class MyHttpContextHelper
    {
        public static string GetQueryParam(HttpContext context, string RequestName)
        {
            return GetQueryParam(context, RequestName, "");
        }

        public static string GetQueryParam(HttpContext context, string RequestName, string DefaultValue)
        {
            if (context.Request.Params[RequestName] == null)
                return DefaultValue;
            else
                return System.Web.HttpUtility.UrlDecode(context.Request.Params[RequestName], System.Text.UnicodeEncoding.GetEncoding("UTF-8"));
        }

        public static string GetPageName(string URL)
        {
            string[] list1 = URL.Split(new string[] { "/" }, StringSplitOptions.RemoveEmptyEntries);
            string strTemp = list1[list1.Length - 1];
            string[] list2 = strTemp.Split(new string[] { "." }, StringSplitOptions.RemoveEmptyEntries);
            return list2[0];
        }

        public static string GetUserIP(HttpContext context)
        {
            string realRemoteIP = "";
            if (context.Request.Headers.AllKeys.Contains("X-real-ip"))
            {
                realRemoteIP = context.Request.Headers["X-real-ip"];
            }
            if (string.IsNullOrEmpty(realRemoteIP))
            {
                realRemoteIP = context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            }
            if (string.IsNullOrEmpty(realRemoteIP))
            {
                realRemoteIP = context.Request.ServerVariables["REMOTE_ADDR"];
            }
            if (string.IsNullOrEmpty(realRemoteIP))
            {
                realRemoteIP = context.Request.UserHostAddress;
            }
            if (string.IsNullOrEmpty(realRemoteIP))
            {
                realRemoteIP = "0.0.0.0";
            }
            return realRemoteIP;
        }
    }
}
